import { getMyProject, getProjectIslandList } from '../../../../api/index'

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    type: {
      type: String,
      value: ''
    }
  },
  observers: {},
  data: {
    personalProject: [],
    companyProjectSelf: [],
    companyProjectJoin: [],
    currentItem: {},
    islandList: []
  },
  methods: {
    beforeOpen () {
      if (parseInt(this.data.type) === 1) this.getMyProject()
      else this.getProjectIslandList()
    },
    async getMyProject () {
      try {
        const { ResultSet } = await getMyProject()
        this.setData({
          personalProject: ResultSet.userData.personalProject,
          companyProjectSelf: ResultSet.userData.companyProjectSelf,
          companyProjectJoin: ResultSet.userData.companyProjectJoin
        })
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    async getProjectIslandList () {
      try {
        const { ResultSet } = await getProjectIslandList()
        this.setData({ islandList: ResultSet.userData.data })
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    select ({ currentTarget: { dataset: { item } } }) {
      this.setData({ currentItem: item })
    },
    close () {
      this.triggerEvent('close')
    },
    confirm () {
      this.triggerEvent('change', this.data.currentItem)
    }
  },
  ready () {}
})